package com.example.fashion.android.FourCompoents.activity;

import android.Manifest;
import android.content.ContentValues;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.fashion.android.base.BaseActivity.BaseActivity;
import com.example.fashion.android.FourCompoents.provider.MyBookProvider;
import com.example.fashion.android.util.LayoutUtil;
import com.example.fashion.android.util.DBUtil.DataBaseHelper;

public class ProviderActivity extends BaseActivity implements LayoutUtil.ClickCallBack {
    private static final String URI = "content://"+ MyBookProvider.AUTHORITY;
    private static final String TAG = "ProviderActivity";

    //最新插入数据的ID
    String newId;

    LinearLayout mRootLayout;

    //功能列表
    private String[] functionArray = {"读取通讯录","添加","查询","更新","删除"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //创建一个线性布局
        mRootLayout = LayoutUtil.getLinearLayout(this, LinearLayout.VERTICAL, Gravity.CENTER);
        setContentView(mRootLayout);
    }

    @Override
    protected void clear() {
    }

    @Override
    public void initView() {
        //显示标题栏
        setSystemTitleName("ContentProvider(内容提供者)");
    }

    @Override
    public void initData() {
        //添加功能列表
        LayoutUtil.addFunctionList(this, mRootLayout, functionArray, this);
    }

    /**
     * 验证用户是否授予了权限
     *
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case 1:
                if (grantResults.length > 0 && grantResults[0] > PackageManager.PERMISSION_GRANTED) {
                    startActivity(new Intent(getApplicationContext(), ContactActivity.class));
                } else {
                    Toast.makeText(this, "暂时没有读取联系人的权限", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }


    @Override
    public void onClickListener(int tag) {
        switch (tag){
            case 0:          //读取联系人
                //验证当前用户的权限
                if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(ProviderActivity.this, new String[]{Manifest.permission.READ_CONTACTS}, 1);
                } else {
                    startActivity(new Intent(getApplicationContext(), ContactActivity.class));
                }
                break;
            case 1:         //插入数据
                ContentValues insertValues = new ContentValues();
                insertValues.put("name","java");
                insertValues.put("author","George Marthin");
                insertValues.put("pages",1040);
                insertValues.put("price",22.85);
                Uri uri = getContentResolver().insert(Uri.parse(URI + "/" + DataBaseHelper.TABLE_BOOK_NAME), insertValues);
                newId = uri.getPathSegments().get(1);
                break;
            case 2:     //查询数据
                Cursor cursor = getContentResolver().query(Uri.parse(URI+"/"+DataBaseHelper.TABLE_BOOK_NAME),null,null,null,null);
                if(cursor!=null){
                    while (cursor.moveToNext()){
                        String name = cursor.getString(cursor.getColumnIndex("name"));
                        String author = cursor.getString(cursor.getColumnIndex("author"));
                        int pages = cursor.getInt(cursor.getColumnIndex("pages"));
                        double price = cursor.getDouble(cursor.getColumnIndex("price"));
                        Log.e(TAG,name+"价值"+price+"元,作者是:"+author+"共"+pages+"页!");
                    }
                    cursor.close();
                }
                break;
            case 3:     //更新数据
                ContentValues updateValues = new ContentValues();
                updateValues.put("name","第一行代码");
                updateValues.put("author","郭霖");
                updateValues.put("pages",570);
                updateValues.put("price",79.00);
                getContentResolver().update(Uri.parse(URI+"/"+DataBaseHelper.TABLE_BOOK_NAME+"/"+newId),updateValues,null,null);
                break;
            case 4:     //删除数据
                getContentResolver().delete(Uri.parse(URI+"/"+DataBaseHelper.TABLE_BOOK_NAME+"/"+newId),null,null);
                break;
        }
    }
}
